package jp.jig.test.alarm;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.inputmethod.InputMethodManager;



public class Util {

	public static final String CALENDAR_FILE_FORMAT_STR = "yyyyMMddHHmmssSSS";
	public static final String CALENDAR_TIME_FORMAT_STR = "HH:mm";

	private static final double EQUATOR = 40075334.2563;

	public static String getTimeStr(long time, String formatStr) {
		Date date = new Date();
		date.setTime(time);
		DateFormat df = new SimpleDateFormat(formatStr);
		return df.format(date);
	}

	public static String getDateStr(int year, int month, int day, String formatStr) {
		Date date = new Date();
		date.setYear(year - 1900);
		date.setMonth(month - 1);
		date.setDate(day);
		DateFormat df = new SimpleDateFormat(formatStr);
		return df.format(date);
	}

	public static int dateCombine(int year, int month, int date) {
		return (year * 10000 + month * 100 + date);
	}

	public static int getCombinedDate() {
		return getCombinedDate(System.currentTimeMillis());
	}
	public static int getCombinedDate(long time) {
		Date d = new Date(time);
		return Util.dateCombine(d.getYear() + 1900, d.getMonth() + 1, d.getDate());
	}
	public static int getCombinedTime() {
		return getCombinedTime(System.currentTimeMillis());
	}
	public static int getCombinedTime(long time) {
		Date d = new Date(time);
		return d.getHours() * 100 + d.getMinutes();
	}


	public static int[] dateSeparate(int date) {
		int y = date / 10000;
		date = date % 10000;
		int m = date / 100;
		int d = date % 100;
		return new int[] {y, m, d};
	}
	public static String getDateStr(int combineDate, String formatStr) {
		int y = combineDate / 10000;
		combineDate = combineDate % 10000;
		int m = combineDate / 100;
		int d = combineDate % 100;
		return getDateStr(y, m, d, formatStr);
	}


	public static String getHHmmTimeStr(int hour, int min) {
		return getHHmmTimeStr(hour * 100 + min);
	}
	public static String getHHmmTimeStr(int time) {//HHmmの4桁時間形式をHH:mm形式にします。
		StringBuffer sb = new StringBuffer();
		int hour = time / 100;
		int min = time % 100;
		if (hour < 0 || 23 < hour|| min < 0 || 59 < min) {//時刻としておかしい
			return null;
		}
		sb.append(hour / 10).append(hour % 10).append(":").append(min / 10).append(min % 10);
		return sb.toString();
	}



	public static float getMetersPerPixel(int zoomLevel, int latE6) {
		return getMetersPerPixel(zoomLevel, (float)(latE6 / 1E6));
	}
	public static float getMetersPerPixel(int zoomLevel, float lat) {
		double mps = EQUATOR / (256 * Math.pow(2, zoomLevel - 1));
		mps = mps * Math.cos(Math.toRadians(lat));//緯度による補正
		return (float)mps;
	}


	public static void setPaintColor(Paint p, int argb) {
		p.setARGB(argb >>> 24, (argb >>> 16) & 0xFF, (argb >>> 8) & 0xFF, (argb & 0xFF));
	}

	private static final int[] DEFAULT_FENCE_RADIUS = new int[] {
		2000000,//0
		2000000,//1
		2000000,
		2000000,
		2000000,
		1000000,//5
		500000,
		200000,
		200000,
		100000,
		50000,///10
		20000,
		10000,
		5000,
		2000,
		1000,//15
		500,
		200,
		200,
		100,
		50,//20
	};

	public static int getDefaultRadius(int zoomLevel) {
		try {
			return DEFAULT_FENCE_RADIUS[zoomLevel];
		} catch (Throwable e) {
			return 50;
		}
	}

	public static int getDefaultZoomLevel(int radius) {
		int level = 5;
		for (; level < 21; level++) {
			if (radius >= DEFAULT_FENCE_RADIUS[level]) {
				break;
			}
		}
		return level;
	}


	public static void hideSoftKeyBoard(Context context, View v) {
		InputMethodManager imm = (InputMethodManager)(context.getSystemService(Context.INPUT_METHOD_SERVICE));
		imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}

	public static void showSoftKeyBoard(Context context, View v) {
		InputMethodManager imm = (InputMethodManager)(context.getSystemService(Context.INPUT_METHOD_SERVICE));
		imm.showSoftInput(v, 0);
	}

}
